package com.ssm.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssm.entity.Student;
import com.ssm.mapper.StudentMapper;
import com.ssm.service.ClassesService;
import com.ssm.service.StudentService;
import com.ssm.utils.PageResult;
import com.ssm.vo.StudentVo;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{

    private final ClassesService classesService;
    public StudentServiceImpl(ClassesService classesService) {
        this.classesService = classesService;
    }

    @Override
    public IPage<Student> conditionFindByPage(Integer page, Integer pageSize, StudentVo studentVo) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        if (studentVo.getName() != null) {
            queryWrapper.like("name", studentVo.getName());
        }
        if (studentVo.getAddr() != null) {
            queryWrapper.like("addr", studentVo.getAddr());
        }
        if (studentVo.getCid() != null) {
            queryWrapper.eq("cid", studentVo.getCid());
        }
        queryWrapper.eq("is_deleted", 0);
        Page<Student> studentPage = page(new Page<>(page, pageSize), queryWrapper);
        studentPage.getRecords().forEach(student -> {
            student.setCname(classesService.getById(student.getCid()).getCname());
        });
        return studentPage;
    }
}
